// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

function on_moveend() {
    center = map.getCenter();
    var params = $H({ x: center.lat(), y: center.lng(), 
                      zoom: map.getZoom() });
    rjs('/map/move', params);
}

function on_mousemove(latlng) {
     $('x').innerHTML = latlng.lat();
     $('y').innerHTML = latlng.lng();
}

function goBack() {
    rjs('/map/back');
}

function goForward() {
    rjs('/map/forward');
}

function goTo(select) {
    option = $F(select);
    if (option == '') return false;
    if (option == 'save') {
        return saveLocation();
    }   
    rjs('/map/goto/' + option);
}

function saveLocation() {
    title = prompt("Please, inform the location's title:", "New Location");
    if (title && title.trim().length > 0) {
        rjs('/map/save', $H({title: title}));
    }
    else
    {
        $('location').value = '';
    }
}

function rjs(url, params) {
    if (params) {
        url = url + '?' + params.toQueryString();
    }
    new Ajax.Request(url, 
                     {asynchronous:true, evalScripts:true})    
}